package com.hl.card.credit.pub.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hl.card.credit.pub.common.RedisKey;
import com.hl.card.credit.pub.common.RedisUtil;
import com.hl.card.credit.pub.dao.AbstractDao;
import com.hl.card.credit.pub.dao.PublicOrdersDao;
import com.hl.card.credit.pub.entity.PublicExpressGoodsInfo;
import com.hl.card.credit.pub.entity.PublicExpressLogisticsInfo;
import com.hl.card.credit.pub.entity.PublicExpressOrder;
import com.hl.card.credit.pub.entity.PublicIOrder;
import com.hl.card.credit.pub.entity.PublicInvoice;
import com.hl.card.credit.pub.entity.PublicOrderLog;
import com.hl.card.credit.pub.entity.PublicOrderTimeNode;
import com.hl.card.credit.pub.entity.PublicOrders;
import com.hl.card.credit.pub.entity.PublicTravelP;
import com.hl.card.credit.pub.entity.PublicTravelPOtherInfo;
import com.hl.card.credit.pub.model.PublicOrdersAllModel;
import com.hl.card.credit.pub.model.PublicOrdersModel;
import com.hl.card.credit.pub.utils.BeanToMapUtils;
import com.hl.card.credit.pub.utils.CommonUtil;
import com.hl.card.credit.pub.utils.FormValid;
import com.hl.card.credit.pub.utils.UuidCodeUtils;

/**
 * 
    * @ClassName: PublicOrdersService
    * @Description: TODO(公共订单业务类)
    * @author wdf
    * @date 2018年3月29日
    *
 */
//@Service
public class PublicOrdersService extends AbstractService<PublicOrders, Integer> {
//	@Autowired
	private PublicOrdersDao<PublicOrders, Integer> dao;

	@Override
	protected AbstractDao<PublicOrders, Integer> getDao() {
		return dao;
	}
	
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private PublicOrderTimeNodeService publicOrderTimeNodeService;
	@Autowired
	private PublicExpressOrderService publicExpressOrderService;
	@Autowired
	private PublicIOrderService publicIOrderService;
	@Autowired
	private PublicTravelPService publicTravelPService;
	@Autowired
	private PublicTravelPOtherInfoService publicTravelPOtherInfoService;
	@Autowired
	private PublicOftenPService publicOftenPService;
	@Autowired
	private PublicOftenPCardInfoService publicOftenPCardInfoService;
	@Autowired
	private PublicOftenPCertInfoService publicOftenPCertInfoService;
	@Autowired
	private PublicExpressLogisticsInfoService publicExpressLogisticsInfoService;
	@Autowired
	private PublicExpressGoodsInfoService publicExpressGoodsInfoService;
	@Autowired
	private PublicOrderLogService publicOrderLogService;
	@Autowired
	private PublicInvoiceService publicInvoiceService;
	/**
	 * 
	    * @Title: addOrderCode
	    * @Description: TODO(生成订单编号)
	    * @param @param param
	    * @param @return    参数
	    * @return String    返回类型
	    * @throws
	    * @remark 需要考虑订单不唯一的问题
	 */
	public String addOrderCode(Map<String, Object> param) {
		String str=param.get("busines_species")+""+param.get("policy_name")+param.get("busines_type");
		int num=redisUtil.incrExpireOrderCode(RedisKey.ORDER_COUNT+param.get("busines_species")+":"+param.get("policy_name")+":"+param.get("busines_type"),1,1);
		Map<String, String> map=UuidCodeUtils.getOrderCode(str,num);
		return map.get("timestr")+","+map.get("code");
	}
	/**
	 * 
	 * @Title: addOrderCode
	 * @Description: TODO(生成订单编号)
	 * @param @param param
	 * @param @return    参数
	 * @return String    返回类型
	 * @throws
	 * @remark 需要考虑订单不唯一的问题
	 */
	public String addOrderCode(PublicOrdersAllModel param) {
		FormValid.validNull(param.getBusines_species(), "业务种类为空");
		FormValid.validNull(param.getPolicy_name(), "商户三字码为空");
		FormValid.validNull(param.getBusiness_type(), "业务类型为空");
		
		String str=param.getBusines_species()+param.getPolicy_name()+param.getBusiness_type();
		int num=redisUtil.incrExpireOrderCode(RedisKey.ORDER_COUNT+param.getBusines_species()+":"+param.getPolicy_name()+":"+param.getBusiness_type(),1,1);
		Map<String, String> map=UuidCodeUtils.getOrderCode(str,num);
		System.out.println("addOrderCode(PublicOrdersAllModel param)param:"+param);
		System.out.println("addOrderCode(PublicOrdersAllModel param)map:"+map);
		System.out.println();
		return map.get("code");
	}
	
	

	/**
	 * 
	 * @Title: getOrderDetail
	 * @Description: TODO(返回订单详情)
	 * @param @param publicOrders
	 * @param @return 参数
	 * @return PublicOrders 返回类型
	 * @throws
	 */
	public PublicOrders getOrderDetail(PublicOrders publicOrders) {

		return dao.getOrderDetail(publicOrders);
	}
	
	/**
	 * 
	    * @Title: addPublicOrdersTypeExpress
	    * @Description: TODO(生成快递订单)
	    * @param @param model
	    * @param @param map
	    * @param @return    参数
	    * @return int    返回类型
	    * @throws
	 */
	public int addPublicOrdersTypeExpress(PublicOrdersAllModel model,Map<String, Object> map){
		PublicOrders publicOrders=PublicOrders.Init(map, model);
		Map<String, Object> param = new HashMap<String, Object>();
		//param.get("busines_species")+""+param.get("policy_name")+param.get("busines_type")
		
		param.put("policy_name",param.get("policy_name"));
		param.put("busines_species", param.get("busines_species"));
		param.put("busines_type", param.get("busines_type"));
		String code = addOrderCode(param);
		publicOrders.setOrder_code(code);
		int num = dao.addPublicOrders(publicOrders);
		return num;
		
	}
	
	/**
	 * 
	    * @Title: queryOrdersList
	    * @Description: TODO(根据订单相关信息查询订单列表)
	    * @param @param publicOrders
	    * @param @return    参数
	    * @return List<PublicOrders>    返回类型
	    * @throws
	 */
	public List<PublicOrders> queryOrdersList(PublicOrdersModel publicOrdersModel){
		return dao.queryOrdersList(publicOrdersModel);
	}
	
	
	
	/**
	 * 
	    * @Title: addPublicOrders
	    * @Description: TODO(订单生成)
	    * @param @param publicOrders
	    * @param @return    参数
	    * @return int    返回类型
	    * @throws
	    * 不成功全部回滚
	 */
	@Transactional(propagation=Propagation.REQUIRED,isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public int addPublicOrders(PublicOrdersAllModel model){
		Map<String, Object> map=new HashMap<String, Object>();
		if(CommonUtil.isEmpty(model.getOrder_code())){
			return 2;
		}
		Date pdate=new Date();
		//公共订单数据
		PublicOrders publicOrders=PublicOrders.Init(map, model);
		dao.addPublicOrders(publicOrders);
		
		//订单日志
		if(CommonUtil.isNotEmpty(model.getPublicOrderLog())){
			PublicOrderLog publicOrderLog=model.getPublicOrderLog();
			publicOrderLogService.add(publicOrderLog);
		}
		
		//订单时间节点 public_order_time_node
		if(CommonUtil.isNotEmpty(model.getPublicOrderTimeNode())){
			PublicOrderTimeNode publicOrderTimeNode=PublicOrderTimeNode.init(map, model);
			publicOrderTimeNodeService.add(publicOrderTimeNode);
		}
		
		//发票
		if(CommonUtil.isNotEmpty(model.getPublicInvoice())){
			PublicInvoice publicInvoice=PublicInvoice.init(map, model);
			publicInvoiceService.add(publicInvoice);
			
		}
		
		//出行人信息
//		PublicTravelP publicTravelP=PublicTravelP.travelPInit(model,map);
//		publicTravelPService.add(publicTravelP);
		
		//包含保险
		if(CommonUtil.isNotEmpty(model.getiList())){
			if(model.getiList().size()>0){
				List<PublicIOrder> publicIOrderList=new ArrayList<PublicIOrder>();
				for (int i = 0; i < model.getiList().size(); i++) {
					Map<String, Object> iMap=model.getiList().get(i);
					//保险数据
					PublicIOrder publicIOrder=BeanToMapUtils.toBean(PublicIOrder.class, iMap);
					publicIOrder.setOrder_code(model.getOrder_code());
					publicIOrderList.add(publicIOrder);
				}
				publicIOrderService.addPublicIOrderBatch(publicIOrderList);
				//写入保险订单
			}
		}
		//快递
		if(CommonUtil.isNotEmpty(model.getExpreesList())){
			if(model.getExpreesList().size()>0){
				
				List<Map<String, Object>> expressList=model.getExpreesList();
				
				List<PublicExpressOrder> publicExpressOrdersList=new ArrayList<PublicExpressOrder>();
				for (int i = 0; i < expressList.size(); i++) {
					PublicExpressOrder publicExpressOrder=BeanToMapUtils.toBean(PublicExpressOrder.class, expressList.get(i));
					//相差的数据在此组装
					
					//写入快递表数据
					publicExpressOrder.setOrder_code(model.getOrder_code());
					publicExpressOrdersList.add(publicExpressOrder);
				}
				publicExpressOrderService.addPublicExpressOrderBatch(publicExpressOrdersList);
			}
		}

		//出行人信息列表
		if(CommonUtil.isNotEmpty(model.getTravelpList())){
			if(model.getTravelpList().size()>0){
				List<Map<String, Object>> travelpList=model.getTravelpList();
				List<PublicTravelP> publicTravlePList=new ArrayList<PublicTravelP>();
				for (int i = 0; i < travelpList.size(); i++) {
					PublicTravelP ptp=BeanToMapUtils.toBean(PublicTravelP.class, travelpList.get(i));
					ptp.setOrder_code(model.getOrder_code());
					ptp.setIs_effect(1);
					publicTravlePList.add(ptp);
//					publicTravelPService.add(ptp);
				}
				publicTravelPService.addPublicTravelPBatch(publicTravlePList);
			}
		}
		//出行人额外信息
		if(CommonUtil.isNotEmpty(model.getTravelpOtherInfoList())){
			if(model.getTravelpOtherInfoList().size()>0){
				List<Map<String, Object>> travelpOtherInfoList=model.getTravelpOtherInfoList();
				
				List<PublicTravelPOtherInfo> list=new ArrayList<PublicTravelPOtherInfo>();
				for (int i = 0; i < travelpOtherInfoList.size(); i++) {
					PublicTravelPOtherInfo ptpoi=BeanToMapUtils.toBean(PublicTravelPOtherInfo.class, travelpOtherInfoList.get(i));
					ptpoi.setOrder_code(model.getOrder_code());
					ptpoi.setIs_effect(1);
					list.add(ptpoi);
//					publicTravelPOtherInfoService.add(ptpoi);
				}
				publicTravelPOtherInfoService.addPublicTravelPOtherInfoBatch(list);
			}
		}
		//快递物流信息
		if(CommonUtil.isNotEmpty(model.getExpressLogisticsInfoList())){
		if(model.getExpressLogisticsInfoList().size()>0){
			List<Map<String, Object>> publicExpressLogisticsInfoList=model.getExpressLogisticsInfoList();
			
			List<PublicExpressLogisticsInfo> list=new ArrayList<PublicExpressLogisticsInfo>();
			for (int i = 0; i < publicExpressLogisticsInfoList.size(); i++) {
				PublicExpressLogisticsInfo peli=BeanToMapUtils.toBean(PublicExpressLogisticsInfo.class, publicExpressLogisticsInfoList.get(i));
				
				list.add(peli);
//				publicExpressLogisticsInfoService.add(peli);
			}
			publicExpressLogisticsInfoService.addPublicExpressLogisticsInfoBatch(list);
		}
		}
		//快递详单
		if(CommonUtil.isNotEmpty(model.getExpressGoodsInfoList())){
			if(model.getExpressGoodsInfoList().size()>0){
				List<Map<String, Object>> expressGoodsInfoList=model.getExpressGoodsInfoList();
				
				List<PublicExpressGoodsInfo> list=new ArrayList<PublicExpressGoodsInfo>();
				for (int i = 0; i < expressGoodsInfoList.size(); i++) {
					PublicExpressGoodsInfo ptpoi=BeanToMapUtils.toBean(PublicExpressGoodsInfo.class, expressGoodsInfoList.get(i));
					
					list.add(ptpoi);
//					publicExpressGoodsInfoService.add(ptpoi);
				}
				publicExpressGoodsInfoService.addPublicExpressGoodsInfoBatch(list);
				
			}
		}
		//预留调用业务表地方
		
		return 1;
	}


	public void updateBySelectiveForUpdate(PublicOrdersAllModel allModel) {
		// TODO Auto-generated method stub
		dao.updateBySelectiveForUpdate(allModel);
	}
	
	/**
	 * 
	    * @Title: updateBySelectiveForAll
	    * @Description: TODO(修改订单状态 以及日志记录和订单时间节点数据 ，一个订单一条记录)
	    * @param @param allModel    参数
	    * @return void    返回类型
	    * @throws
	 */
	public void updateBySelectiveForAll(PublicOrdersAllModel allModel) {
		PublicOrderTimeNode publicOrderTimeNode=allModel.getPublicOrderTimeNode();
		
		if(CommonUtil.isNotEmpty(publicOrderTimeNode))
			publicOrderTimeNodeService.add(publicOrderTimeNode);
		
		PublicOrderLog publicOrderLog=allModel.getPublicOrderLog();
		if(CommonUtil.isNotEmpty(publicOrderLog))
			publicOrderLogService.updateBySelective(publicOrderLog);
		
		dao.updateBySelectiveForUpdate(allModel);
	}


	public PublicOrders queryDetailForUpdate(PublicOrders order) {
		// TODO Auto-generated method stub
		return dao.queryDetailForUpdate(order);
	}
}
